package evaluation;

/**
 * 打印两个链表的公共部分
 */
public class LinkQuestion {
    static class Node {
        private int Data;// 数据域
        private Node Next;// 指针域
        public Node(int Data) {
            // super();
            this.Data = Data;
        }
        public int getData() {
            return Data;
        }
        public void setData(int Data) {
            this.Data = Data;
        }

        public Node getNext() {
            return Next;
        }
        public void setNext(Node Next) {
            this.Next = Next;
        }
    }

    public static void main(String[] args) {
        Node head = new Node(0);
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);

        head.setNext(node1);
        node1.setNext(node2);
        node2.setNext(node3);

        Node head2 = new Node(0);
        Node node12 = new Node(2);
        Node node13 = new Node(4);

        head2.setNext(node12);
        node12.setNext(node13);

        printlnLink(head,head2);

    }

    private static void printlnLink(Node head, Node head2) {
        while (head!=null && head2!=null){
            if (head.getData() == head2.getData()){
                System.out.println(head.getData());
                head = head.getNext();
                head2 = head2.getNext();
            }else {
                if (head.getData() > head2.Data){
                    head2 = head2.getNext();
                }else {
                    head = head.getNext();
                }
            }
        }
    }
}
